<?php

#	This file is part of SmartWebIF.
#	
#	SmartWebIF is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	SmartWebIF is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with SmartWebIF.  If not, see <http://www.gnu.org/licenses/>.

$config = include 'config.php';

if (($config["password"] <> '') and ($_COOKIE["auth"] <> $config["password"]) or ($_COOKIE["time"] <> '' and $_COOKIE["time"] < time())) die();

$pages = $_GET["rows"];
$sidx = $_GET["sidx"];
$sord = $_GET["sord"];
$satid = $_GET["satid"];

if(!$sidx) $sidx = 3;
if (!$sord) $sord = 'asc';

$servicetype = ' (servicetype = \'\'';
if ($config["data"] == '1') $servicetype = $servicetype.' or servicetype = 192';
if ($config["tv"] == '1') $servicetype = $servicetype.' or servicetype = 1';
if ($config["radio"] == '1') $servicetype = $servicetype.' or servicetype = 2';
$servicetype = $servicetype.')';

$ecmpid = ' (ecmpid = \'\'';
if ($config["fta"] == '1') $ecmpid = $ecmpid.' or ecmpid = 0';
if ($config["crypted"] == '1') $ecmpid = $ecmpid.' or ecmpid <> 0';
$ecmpid = $ecmpid.')';

$db = new SQLite3($config[data_path].'\smartdvb.db');
$sql = "select sat_id, ckey, sid, tid, nid, servicename, serviceprovidername, frequency from channels where $servicetype and $ecmpid and sat_id = $satid order by $sidx $sord";
$rs = $db->query($sql) or die();

$i=0;
while($row = $rs->fetchArray(SQLITE3_ASSOC))
	{
	$encode->rows[$i]["id"] = $row["ckey"];
    $encode->rows[$i]["cell"] = array($row["sat_id"],$row["sid"],$row["tid"],$row["nid"],$row["servicename"],$row["serviceprovidername"],$row["frequency"]);
    $i++;
	}

echo json_encode($encode);
?>